package com.firewolf.springboot.controller;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	// 没有指定为主数据源.
	@Autowired
	private DataSource dataSource;

	@Autowired
	@Qualifier("ds1")
	private DataSource dataSource1;

	@Autowired
	@Qualifier("ds2")
	private DataSource dataSource2;
	
	@Autowired
	@Qualifier("ds3")
	private DataSource dataSource3;
	
	
	@RequestMapping("/ds")
	public String addToDs(){
		createTable(dataSource);
		return "success";
	}
	@RequestMapping("/ds1")
	public String addToDs1(){
		createTable(dataSource1);
		return "success";
	}
	@RequestMapping("/ds2")
	public String addToDs2(){
		createTable(dataSource2);
		return "success";
	}
	
	@RequestMapping("/ds3")
	public String addToDs3(){
		createTable(dataSource3);
		return "success";
	}
	
	private void createTable(DataSource ds){
		JdbcTemplate jt = new JdbcTemplate(ds);
		String sql = "create table students(id int primary key auto_increment,name varchar(20),age int)";
		jt.execute(sql);
	}
}
